import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:wu_you/utils/login_util.dart';
import 'package:wu_you/widgets/button_view.dart';
import 'package:wu_you/widgets/loading_view.dart';
import 'package:wu_you/utils/extension_utils.dart';
typedef HttpSuccessBuilder<T> = Widget Function(BuildContext context, T data);
typedef ErrorRefresh<T> = Future<T> Function();
class FutureBuilderWidget<T> extends StatefulWidget {
  final Future<T> future;
  final HttpSuccessBuilder<T> builder;
  final WidgetBuilder emptyBuilder;
  final bool showLoading;
  final ErrorRefresh<T> errorCallback;
  const FutureBuilderWidget(
      {Key key,
      @required this.future,
      @required this.builder,
      this.emptyBuilder,
      this.showLoading = true,this.errorCallback})
      : super(key: key);

  @override
  _FutureBuilderWidgetState<T> createState() => _FutureBuilderWidgetState<T>();
}

class _FutureBuilderWidgetState<T> extends State<FutureBuilderWidget<T>> {
  Future<T> _future;

  Future<T> refresh({Future<T> future}) async {
    setState(() {
      _future = future ?? widget.future;
    });
  }

  @override
  void initState() {
    _future = widget.future;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<T>(
        builder: (context, snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.none:
              return widget.emptyBuilder ?? Container();
              break;
            case ConnectionState.waiting:
              return widget.showLoading ? LoadingView() : Container();
              break;
            case ConnectionState.active:
              return widget.showLoading ? LoadingView() : Container();
              break;
            default:
              if (snapshot.hasError) {
                if (snapshot.error is DioError) {
                  DioError dioError = snapshot.error;
                  if (dioError.response != null) {
                    if (dioError.response.statusCode ==
                        HttpStatus.unauthorized) {
                      // LoginUtil.logout();
                    }
                  }
                  if(widget.errorCallback != null)
                    return Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                      Text(dioError.message),
                      ElevatedButtonWidget(onPressed: (){
                        setState(() {
                          _future = widget.errorCallback();
                        });
                      } , child: Text("重新请求",style: TextStyle(fontSize: 12),),size: Size(60, 30),)
                    ],);
                  return Center(
                      child: Container(
                          padding: EdgeInsets.all(10),
                          decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(5)),
                          child: Text(dioError.message)).addGesture(() {
                            Navigator.pop(context);
                      }));
                }
                return Center(child: Text(snapshot.error.toString()));
              } else {
                return widget.builder(context, snapshot.data);
              }
              break;
          }
        },
        future: _future);
  }
}
